﻿using CPPEI.Coolzon.UI.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace CPPEI.Coolzon.Designer.Views
{
    public partial class DesignView
    {
        /// <summary>
        /// 发布创建区域的事件
        /// </summary>
        /// <param name="parentName"></param>
        /// <param name="areaPart"></param>
        private void RaiseNCLCreateAreaEvent(string parentName, Coolzon.UI.AreaPart areaPart)
        {
            Designtime.Metadata.Area areaMetadata = new Designtime.Metadata.Area(Guid.NewGuid(), areaPart.Name, areaPart.XCaption, false);
            areaMetadata.RT_Area.Type = SelectedPartLibItem.Name;
            areaMetadata.SetLeft(areaPart.XLeft);
            areaMetadata.SetTop(areaPart.XTop);
            areaMetadata.SetWidth(areaPart.XWidth);
            areaMetadata.SetHeight(areaPart.XHeight);

            // 添加到场景元数据中
            object parentMetadata = Designtime.Metadata.MetadataUtil.GetMetadataNodeByName(Solution, parentName);
            if (parentMetadata is Designtime.Metadata.Scene)
            {
                (parentMetadata as Designtime.Metadata.Scene).AddArea(areaMetadata);
            }
            else if (parentMetadata is Designtime.Metadata.Area)
            {
                (parentMetadata as Designtime.Metadata.Area).AddArea(areaMetadata);
            }
            
            Event.UI.UICreateAreaEvent @event = new Event.UI.UICreateAreaEvent(parentName, areaMetadata);
            Event.UI.UIEventBus.PublishAsync(@event);
        }

        /// <summary>
        /// 发布创建组件的事件
        /// </summary>
        /// <param name="parentName"></param>
        /// <param name="areaPart"></param>
        private void RaiseNCLCreatePartEvent(string parentName, Coolzon.UI.Part part)
        {
            //Designtime.Metadata.Part partMetadata = new Designtime.Metadata.Part(Guid.NewGuid(), part.Name, part.XCaption);
            ////partMetadata.RT_Part.Type = 
            //partMetadata.RT_Part.Type = SelectedPartLibItem.Name;
            //partMetadata.SetLeft(part.XLeft);
            //partMetadata.SetTop(part.XTop);
            //partMetadata.SetWidth(part.XWidth);
            //partMetadata.SetHeight(part.XHeight);


            //object parentMetadata = Designtime.Metadata.MetadataUtil.GetMetadataNodeByName(Solution, parentName);
            //if (parentMetadata is Designtime.Metadata.Area)
            //{
            //    (parentMetadata as Designtime.Metadata.Area).AddPart(partMetadata);
            //}
            //else if (parentMetadata is Designtime.Metadata.Part)
            //{
            //    (parentMetadata as Designtime.Metadata.Part).AddPart(partMetadata);
            //}


            //Event.UI.UICreatePartEvent @event = new Event.UI.UICreatePartEvent(parentName, partMetadata);
            //Event.UI.UIEventBus.PublishAsync(@event);
        }

        private void _RaiseRemovedEvent(Coolzon.UI.Part part)
        {
            if(Removed!=null)
            {
                Removed(part);
            }
            if (Dirtied != null)
            {
                Dirtied();
            }
        }

        private void _RaiseAddedEvent(Coolzon.UI.Part part)
        {
            if (Added != null)
            {
                Added(part);
            }
            if(Dirtied != null)
            {
                Dirtied();
            }
        }

        private void _RaiseSceneOpenedEvent(Coolzon.UI.ScenePart scene)
        {
            if (Opened != null)
            {
                Opened(scene);
            }
        }
    }
}
